package master.userInterface;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

import javax.swing.*;

import latexExport.QuantumRegisterLaTeXWriter;
import master.ComplexNumber;
import master.QuantumRegister;
import master.quantumComputerIO.register.QuantumRegisterIO;
import master.quantumComputerIO.register.QuantumRegisterException;

import master.quantumComputerIO.*;



/**
 * a panel giving a user various options for manipulating a quantum register using a user interface
 * @author Luke Dobinson
 *
 */

public class QuantumRegisterButtonToolbar extends JToolBar implements ActionListener{
	private final QuantumRegister r;
	private Component myOwner;
	public static int randomiseSeed = 0;//use for debugging
	JButton [] buttons = new JButton[]{
			new JButton("Default"),
			new JButton("Normalise"),
			new JButton("Collapse"),
			new JButton("Randomize"),
			new JButton("Save"),
			new JButton("Load"),
			new JButton("Export as LaTeX")
	};
	
	public QuantumRegisterButtonToolbar(QuantumRegister parent, Component myOwner){
		setFloatable(false);
		r = parent;
		this.myOwner = myOwner;
		for(JButton p : buttons){
			p.addActionListener(this);
			add(p);
		}
	}
	
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getActionCommand()=="Default"){
			r.setZero();
			r.setCoefficient(new ComplexNumber(1,0), 0);
		}if(arg0.getActionCommand()=="Normalise"){
			r.normalise();
		}else if(arg0.getActionCommand()=="Collapse"){
			r.collapseToEigenstate();
		}else if(arg0.getActionCommand()=="Randomize"){
			Random myRnd;
			if(randomiseSeed!=0)myRnd = new Random(randomiseSeed);
			else myRnd = new Random();
			for(int i = 0 ; i < r.getDimension() ; i++)
				r.setCoefficient(new ComplexNumber(myRnd.nextFloat()-0.5f,myRnd.nextFloat()-0.5f), i);
			r.normalise();
		}else if(arg0.getActionCommand()=="Save"){
			try{
				QuantumRegisterIO.saveDialog(r);
			}catch(QuantumRegisterException e){
				
			}
		}else if(arg0.getActionCommand()=="Load"){
			try{
			r.copyValues(
					QuantumRegisterIO.loadDialog()
					);
			}catch(QuantumRegisterException e){
				
			}
		}else if(arg0.getActionCommand()=="Export as LaTeX"){
			FileDialog c = new FileDialog(new JFrame());
			c.setMode(FileDialog.SAVE);
			c.setVisible(true);
			String t = c.getDirectory() + c.getFile();
			try{
				File f = new File(t);
				FileWriter w = new FileWriter(f);
				QuantumRegisterLaTeXWriter q = new QuantumRegisterLaTeXWriter(w);
				
				q.write("\\documentclass{article}");
				q.newLine();
				q.write("\\begin{document}");
				q.newLine();
				q.write("\\section{"+r.toString()+"}");
				q.newLine();
				q.writeRegister(r);
				q.newLine();
				q.write("\\end{document}");
				q.close();
				w.close();
				
			}catch(IOException e){
				JOptionPane.showMessageDialog(null, "Error");
			}
			c.dispose();
		}
		for(Frame f : JFrame.getFrames())f.repaint();//repaint all frames in program
	}
}
